{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ceb3b209-e12f-436c-b9e6-3d429a86ff53",
   "metadata": {},
   "outputs": [],
   "source": [
    "# concatenate之后的结果, 和分别计算的结果是一样的. \n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7cca1b74-d461-42c1-a39f-90905cac76df",
   "metadata": {},
   "outputs": [],
   "source": [
    "X, W_xh = torch.normal(0, 1, (3, 1)), torch.normal(0, 1, (1, 4)) # (3, 1) . (1, 4) -> (3, 4)\n",
    "H, W_hh = torch.normal(0, 1, (3, 4)), torch.normal(0, 1, (4, 4)) # (3, 4) .(4, 4) -> (3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ae47254b-f1cf-4027-bab4-22e7b13a7152",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 3.5408, -6.8829, -1.3655,  1.0352],\n",
       "        [ 0.0463,  2.6232, -1.2455,  0.7298],\n",
       "        [ 0.1551,  0.8378, -0.0315, -0.9864]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分别点乘然后相加\n",
    "torch.matmul(X, W_xh) + torch.matmul(H, W_hh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "91d61481-0f85-406f-ba40-650ebc328eec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 5])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# concatenate之后再做点乘.\n",
    "torch.cat((X, H), dim=1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f576b884-0443-4cb7-a0f7-b441d684ae2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([5, 4])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cat((W_xh, W_hh), dim=0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5b314f7d-2742-47ca-b308-811ae29a3c8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 3.5408, -6.8829, -1.3655,  1.0352],\n",
       "        [ 0.0463,  2.6232, -1.2455,  0.7298],\n",
       "        [ 0.1551,  0.8378, -0.0315, -0.9864]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.matmul((torch.cat((X, H), dim=1)), torch.cat((W_xh, W_hh), dim=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4eb13011-aea9-4171-ac1f-d248c158325e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
